﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using Resources;

namespace Wicresoft.RDW.Manage
{
    public partial class ParameterManage : BasePage_BackEnd
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public override void InitControlResource()
        {
            lbtnSave.Text = ManageResource.mgr_btnOK;
            lbtnCancel.Text = ManageResource.mgr_btnCancel;

            rfvApplyBeforeHour.ErrorMessage = ManageResource.ApplyBeforeHour + CommonResource.NotNull;
            rfvApproveWaitingDay.ErrorMessage = ManageResource.ApproveWaitingDay + CommonResource.NotNull;
            rfvApplyPeriodMinute.ErrorMessage = ManageResource.ApplyPeriodMinute + CommonResource.NotNull;
            rfvCancelBeforeHour.ErrorMessage = ManageResource.CancelBeforeHour + CommonResource.NotNull;
            rfvFeedbackEmailRemindDay.ErrorMessage = ManageResource.FeedbackEmailRemindDay + CommonResource.NotNull;

            rfvCreditScoreIncrease.ErrorMessage = ManageResource.CreditScoreIncrease + CommonResource.NotNull;
            rfvCreditScoreDecrease.ErrorMessage = ManageResource.CreditScoreDecrease + CommonResource.NotNull;
            rfvMinCreditScore.ErrorMessage = ManageResource.MinCreditScore + CommonResource.NotNull;
            rfvMaxCreditScore.ErrorMessage = ManageResource.MaxCreditScore + CommonResource.NotNull;
            revMaxCreditScore.ErrorMessage = ManageResource.MaxCreditScore + CommonResource.ErrorFormat;
            revMinCreditScore.ErrorMessage = ManageResource.MaxCreditScore + CommonResource.ErrorFormat;
            revCancelBeforeHour.ErrorMessage = ManageResource.CancelBeforeHour + CommonResource.ErrorFormat;
            revApplyPeriodMinute.ErrorMessage = ManageResource.ApplyPeriodMinute + CommonResource.ErrorFormat;
            revApplyBeforeHour.ErrorMessage = ManageResource.ApplyBeforeHour + CommonResource.ErrorFormat;
            revApproveWaitingDay.ErrorMessage = ManageResource.ApproveWaitingDay + CommonResource.ErrorFormat;
            revFeedbackEmailRemindDay.ErrorMessage = ManageResource.FeedbackEmailRemindDay + CommonResource.ErrorFormat;

            revCreditScoreIncrease.ErrorMessage = ManageResource.CreditScoreIncrease + CommonResource.ErrorFormat;
            revCreditScoreDecrease.ErrorMessage = ManageResource.CreditScoreDecrease + CommonResource.ErrorFormat;

            txtApplyBeforeHour.Text = SysSettingPresent.ApplyBeforeHour;
            txtApproveWaitingDay.Text = SysSettingPresent.ApproveWaitingDay;
            txtApplyPeriodMinute.Text = SysSettingPresent.ApplyPeriodMinute;
            txtCancelBeforeHour.Text = SysSettingPresent.CancelBeforeHour;
            txtFeedbackEmailRemindDay.Text = SysSettingPresent.FeedbackEmailRemindDay;

            txtCreditScoreIncrease.Text = SysSettingPresent.CreditScoreIncrease;
            txtCreditScoreDecrease.Text = SysSettingPresent.CreditScoreDecrease;
            txtMinCreditScore.Text = SysSettingPresent.MinCreditScore;
            txtMaxCreditScore.Text = SysSettingPresent.MaxCreditScore;
            //txtApplicationUrl.Text = SysSettingPresent.ApplicationUrl;
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            if (int.Parse(txtMinCreditScore.Text) >= int.Parse(txtMaxCreditScore.Text))
            {
                lblMinCreditScore.Text = ManageResource.Validator_MinCreditScore;
                lblMaxCreditScore.Text = ManageResource.Validator_MaxCreditScore;
                lblMinCreditScore.Visible = true;
                lblMaxCreditScore.Visible = true;
                return;
            }

            var cancelBeforeHour = SysSettingPresent.GetByCondition(p => p.Key == "CancelBeforeHour");
            cancelBeforeHour.Value = txtCancelBeforeHour.Text;
            SysSettingPresent.Update(cancelBeforeHour);

            var approveWaitingDay = SysSettingPresent.GetByCondition(p => p.Key == "ApproveWaitingDay");
            approveWaitingDay.Value = txtApproveWaitingDay.Text;
            SysSettingPresent.Update(approveWaitingDay);

            var applyPeriodMinute = SysSettingPresent.GetByCondition(p => p.Key == "ApplyPeriodMinute");
            applyPeriodMinute.Value = txtApplyPeriodMinute.Text;
            SysSettingPresent.Update(applyPeriodMinute);

            var feedbackEmailRemindDay = SysSettingPresent.GetByCondition(p => p.Key == "FeedbackEmailRemindDay");
            feedbackEmailRemindDay.Value = txtFeedbackEmailRemindDay.Text;
            SysSettingPresent.Update(feedbackEmailRemindDay);

            var creditScoreIncrease = SysSettingPresent.GetByCondition(p => p.Key == "CreditScoreIncrese");
            creditScoreIncrease.Value = txtCreditScoreIncrease.Text;
            SysSettingPresent.Update(creditScoreIncrease);

            var creditScoreDecrease = SysSettingPresent.GetByCondition(p => p.Key == "CreditScoreDecrese");
            creditScoreDecrease.Value = txtCreditScoreDecrease.Text;
            SysSettingPresent.Update(creditScoreDecrease);

            var applyBeforeHour = SysSettingPresent.GetByCondition(p => p.Key == "ApplyBeforeHour");
            applyBeforeHour.Value = txtApplyBeforeHour.Text;
            SysSettingPresent.Update(applyBeforeHour);

            var minCreditScore = SysSettingPresent.GetByCondition(p => p.Key == "MinCreditScore");
            minCreditScore.Value = txtMinCreditScore.Text;
            SysSettingPresent.Update(minCreditScore);

            var maxCreditScore = SysSettingPresent.GetByCondition(p => p.Key == "MaxCreditScore");
            maxCreditScore.Value = txtMaxCreditScore.Text;
            SysSettingPresent.Update(maxCreditScore);

            //var approvedUrl = SysSettingPresent.GetByCondition(p => p.Key == "ApplicationUrl");
            //approvedUrl.Value = txtApplicationUrl.Text;
            //SysSettingPresent.Update(approvedUrl);
            Response.Redirect("/Manage/ManagementCenter.aspx");
        }

        protected void lbtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Manage/ManagementCenter.aspx");
        }
    }
}